package com.tianpengcc.test;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.util.Factory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.junit.Assert;
import org.junit.Test;

public class testHelloWorld {

	@Test
	public void test() {
		//获取SecurityManager工厂，此处初始化SecurityManager
		Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");
		
		//得到SecurityManager实例，并绑定给SecurityUtil
		SecurityManager securityManager = factory.getInstance();
		SecurityUtils.setSecurityManager(securityManager);
		
		//创建Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
		Subject currentUser = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken("tian","123") ;
		
		//登录
		try {
			currentUser.login(token);
			System.out.println("登录成功！");
		} catch (AuthenticationException e) {
			//身份验证失败
			System.out.println("登录失败");
		}
		
		//断言用户是否登录
		Assert.assertTrue(currentUser.isAuthenticated());
		
		//退出
		currentUser.logout();
	}

}
